import {reqAddOrUpdateShopCart, reqDeleteAllShopCart, reqDeleteShopCart, reqShopCartList, reqUpdateAllIsChecked, reqUpdateOneIsChecked} from '@/api'

const state = {
  shopCartList:[]
}

const mutations = {
  RECEIVE_SHOPCARTLIST(state,shopCartList){
    state.shopCartList = shopCartList
  }
}

const actions = {
    async addOrUpdateShopCart({commit},{skuId,skuNum}){
    const result = await reqAddOrUpdateShopCart(skuId,skuNum)
    if(result.code === 200){
      return 'ok'
    }else{
      return Promise.reject(new Error('failed'))
    }
  },

  async getShopCartList({commit}){
    const result = await reqShopCartList()
    if (result.code === 200) {
      return commit('RECEIVE_SHOPCARTLIST',result.data)
    }
  },

  // 请求修改单个的选中状态
  async updateOneIsChecked({commit},{skuId,isChecked}){
    const result = await reqUpdateOneIsChecked(skuId,isChecked)
    if (result.code === 200) {
      return 'ok'
    }else{
      return Promise.reject(new Error('failed'))
    }
  },

  // 请求修改多个的选中状态
  async updateAllIsChecked({commit},{isChecked,skuIds}){
    const result = await reqUpdateAllIsChecked(isChecked,skuIds)
    if (result.code === 200) {
      return 'ok'
    }else{
      return Promise.reject(new Error('failed'))
    }
  },

  // 删除单个商品
  async deleteShopCart({commit},skuId){
    const reqult = await reqDeleteShopCart(skuId)
    if (reqult.code === 200) {
      return 'ok'
    }else{
      return Promise.reject(new Error('failed'))
    }
  },

  //删除选中的多个购物车
  async deleteAllShopCart({commit},skuIds){
    const result = await reqDeleteAllShopCart(skuIds)
    if (result.code === 200) {
      return 'ok'
    }else{
      return Promise.reject(new Error('failed'))
    }
  } 
}

const getters = {}

export default {
    state,
    mutations,
    actions,
    getters
}